﻿using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DAL.DataAccessObject
{
    public class NhaPhanPhoiDAO:DataAccess
    {
        public void NPP_AddNew(NhaPhanPhoi npp)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO NhaPhanPhoi VALUES (@MaNPP,@TenNPP,@DiaChi,@SDT,@BatDauHD,@KetThucHD,@LanKyHD)", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaNPP", SqlDbType.Char)).Value = npp.MaNPP;
            cmd.Parameters.Add(new SqlParameter("@TenNPP", SqlDbType.VarChar)).Value = npp.TenNPP;
            cmd.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar)).Value = npp.DiaChi;
            cmd.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar)).Value = npp.SDT;
            cmd.Parameters.Add(new SqlParameter("@BatDauHD", SqlDbType.Date)).Value = npp.NgayBatDauHD.Date;
            cmd.Parameters.Add(new SqlParameter("@KetThucHD", SqlDbType.Date)).Value = npp.NgayKetThucHD.Date;
            cmd.Parameters.Add(new SqlParameter("@LanKyHD", SqlDbType.Int)).Value = npp.LanKyHD;
            cmd.ExecuteNonQuery();
        }

        public void NPP_Update(NhaPhanPhoi npp)
        {
            SqlCommand cmd = new SqlCommand("UPDATE NhaPhanPhoi SET DiaChi=@DiaChi,SDT=@SDT,BatDauHD=@BatDauHD,KetThucHD=@KetThucHD,LanKyHD=@LanKyHD WHERE MaNPP=@MaNPP", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaNPP", SqlDbType.Char)).Value = npp.MaNPP;
            cmd.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.VarChar)).Value = npp.DiaChi;
            cmd.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar)).Value = npp.SDT;
            cmd.Parameters.Add(new SqlParameter("@BatDauHD", SqlDbType.Date)).Value = npp.NgayBatDauHD.Date;
            cmd.Parameters.Add(new SqlParameter("@KetThucHD", SqlDbType.Date)).Value = npp.NgayKetThucHD.Date;
            cmd.Parameters.Add(new SqlParameter("@LanKyHD", SqlDbType.Int)).Value = npp.LanKyHD;
            cmd.ExecuteNonQuery();
        }
        public void NPP_Delete(NhaPhanPhoi npp)
        {
            SqlCommand cmd = new SqlCommand("DELETE NhaPhanPhoi WHERE MaNPP=@MaNPP", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@MaNPP", SqlDbType.Char)).Value = npp.MaNPP;
            cmd.ExecuteNonQuery();
        }
        public List<NhaPhanPhoi> NPP_GetData()
        {
            List<NhaPhanPhoi> listNPP = new List<NhaPhanPhoi>();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM NhaPhanPhoi", GetConnection());
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NhaPhanPhoi npp = new NhaPhanPhoi();
                npp.MaNPP = dt.Rows[i]["MaNPP"].ToString();
                npp.TenNPP= dt.Rows[i]["TenNPP"].ToString();
                npp.NgayBatDauHD = Convert.ToDateTime(dt.Rows[i]["BatDauHD"]);
                npp.NgayKetThucHD= Convert.ToDateTime(dt.Rows[i]["KetThucHD"]);
                npp.SDT = dt.Rows[i]["SDT"].ToString();
                npp.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                npp.LanKyHD = Convert.ToInt32(dt.Rows[i]["LanKyHD"]);
                listNPP.Add(npp);
            }
            return listNPP;
        }
        public List<NhaPhanPhoi> NPP_SearchName(string a)
        {
            List<NhaPhanPhoi> listnpp = new List<NhaPhanPhoi>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM NhaPhanPhoi", GetConnection());
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (a == dr["TenNPP"].ToString().Trim())
                {
                    NhaPhanPhoi npp = new NhaPhanPhoi();
                    npp.MaNPP = dr["MaNPP"].ToString();
                    npp.NgayBatDauHD = Convert.ToDateTime(dr["BatDauHD"]);
                    npp.NgayKetThucHD = Convert.ToDateTime(dr["KetThucHD"]);
                    npp.TenNPP = dr["TenNPP"].ToString();
                    npp.LanKyHD = Convert.ToInt32(dr["LanKyHD"]);
                    npp.DiaChi = dr["DiaChi"].ToString();
                    npp.SDT = dr["SDT"].ToString();
                    listnpp.Add(npp);
                }
            }
            return listnpp;
        }

        public bool NPP_CheckName(string a)
        {
            SqlCommand cmd = new SqlCommand("SELECT TenNPP FROM NhaPhanPhoi", DataAccess.GetConnection());
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (a == dr["TenNPP"].ToString()) return true;
            }
            return false;
        }

        public DataView SelectViewAllNPP()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM NhaPhanPhoi", GetConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return new DataView(dt);
        }
    }
}
